package com.gang.ssm.bos.system.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gang.crm.bean.Customer;
import com.gang.ssm.bos.common.Page;
import com.gang.ssm.bos.core.anotation.Permission;
import com.gang.ssm.bos.core.toolbox.ajax.AjaxResult;
import com.gang.ssm.bos.system.bean.Decidedzone;
import com.gang.ssm.bos.system.bean.Subarea;
import com.gang.ssm.bos.system.service.CustomerService;
import com.gang.ssm.bos.system.service.DecidedzoneService;
import com.gang.ssm.bos.system.service.SubareaService;

@RequestMapping("/decidedzone")
@Controller
public class DecidedzoneController extends BosController{
	
	@Autowired
	private DecidedzoneService decidedzoneService;
	
	@Autowired
	private SubareaService subareaService;
	
	@Autowired
	private CustomerService customerService;
	
	@RequestMapping("/decidedzone")
	public String input(Integer id, Map<String, Object> map) {
		
		List<Integer> subareaIds = null;
		Decidedzone decidedzone = null;
		//获取未关联到定区的分区
		List<Subarea> subareas = subareaService.findDecidedzoneIdIsNull();
		
		if(id != null) {
			decidedzone = decidedzoneService.findWithSubareasById(id);
			map.put("decidedzone", decidedzone);
			
			subareaIds = subareaService.findByDecidedzoneId(id);
		}
		
		
		if(subareaIds != null) {
			//把定区已关联的分区添加到未关联分区的定区中， 目的是回显
			subareas.addAll(decidedzone.getSubareas());
			for (Subarea subarea : subareas) {
				if(subareaIds.contains(subarea.getId())) {
					subarea.setDecededzoneHas(true);
				}
			}
		}
		map.put("subareas", subareas);
		
		return "system/base/decidedzone";
	}
	
	@RequestMapping("/list")
	public String list(Page<Decidedzone> page, Map<String, Object> map) {
		
		decidedzoneService.pageQuery(page);
		map.put("page", page);
		return "system/base/decidedzoneList";
	}
	
	@Permission({ADMIN})
	@ResponseBody
	@RequestMapping("/save")
	public AjaxResult save(Decidedzone decidedzone, String subareaIds) {
		try {
			decidedzoneService.save(decidedzone, StringUtils.isBlank(subareaIds)?null:subareaIds.split(","));
			return success("保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fail("保存失败!");
	}
	
	@Permission({ADMIN})
	@ResponseBody
	@RequestMapping("/delete")
	public AjaxResult delete(Integer id){
		try {
			decidedzoneService.deleteById(id);
			return success("删除成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fail("删除失败!");
	}
	
	@RequestMapping(value="/relationCustomers",method=RequestMethod.GET)
	public String associationCustomersUI(@RequestParam("id")Integer decidedzoneId, 
			Map<String, Object> map) {
		
		List<Customer> customers = customerService.findNoAssociationCustomers();
		List<Customer> associationCustomers = null;
		if(decidedzoneId != null) {
			associationCustomers = customerService.findHasAssociationCustomers(decidedzoneId);
			//把已关联的客户和未关联的客户合并
			customers.addAll(associationCustomers);
			
			for(Customer customer : customers) {
				for(Customer hasAssCustomer : associationCustomers) {
					//设置hasAss字段为true,方便页面回显
					if(customer.getId() == hasAssCustomer.getId()) {
						customer.setHasAss(true);
					}
				}
			}
		}
		map.put("customers", customers);
		
		return "system/base/relationCustomers";
	}
	
	@Permission({ADMIN})
	@ResponseBody
	@RequestMapping("/relationCustomers")
	public AjaxResult relationCustomers(String customerIds, Integer decidedzoneId) {
		
		try {
			customerService.assignCustomersToDecidedZone(customerIds, decidedzoneId);
			return success("关联成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fail("关联失败!");
	}

}
